package com.tjvib.sensor;

import androidx.annotation.NonNull;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LpmsB2SensorData extends SensorData {

    public int imuId = 0;
    public int frameNumber;
    public float[] euler = new float[3];
    public float[] angVel = new float[3];
    public float pressure;
    public float altitude;
    public float batteryLevel;
    public float batteryVoltage;
    public int chargingStatus;
    public float temperature;
    public float heave;

    @Override
    void reset() {
        super.reset();
        imuId = 0;
        frameNumber = 0;
        pressure = 0;
        altitude = 0;
        batteryLevel = 0;
        batteryVoltage = 0;
        temperature = 0;
        chargingStatus = 0;
        heave = 0;

        for (int i = 0; i < 3; i++) angVel[i] = 0;
        for (int i = 0; i < 3; i++) euler[i] = 0;
    }

    public LpmsB2SensorData() {
        reset();
    }

    public LpmsB2SensorData(LpmsB2SensorData d) {
        super(d);
        imuId = d.imuId;
        frameNumber = d.frameNumber;
        pressure = d.pressure;
        altitude = d.altitude;
        batteryLevel = d.batteryLevel;
        batteryVoltage = d.batteryVoltage;
        temperature = d.temperature;
        chargingStatus = d.chargingStatus;
        heave = d.heave;

        for (int i = 0; i < 3; i++) angVel[i] = d.angVel[i];
        for (int i = 0; i < 3; i++) euler[i] = d.euler[i];
    }

    public LpmsB2SensorData(JSONObject jsonObject) throws JSONException {
        super(jsonObject);
        imuId = jsonObject.getInt("imuId");
        frameNumber = jsonObject.getInt("frameNumber");
        pressure = (float) jsonObject.getDouble("pressure");
        altitude = (float) jsonObject.getDouble("altitude");
        batteryLevel = (float) jsonObject.getDouble("batteryLevel");
        batteryVoltage = (float) jsonObject.getDouble("batteryVoltage");
        chargingStatus = jsonObject.getInt("chargingStatus");
        temperature = (float) jsonObject.getDouble("temperature");
        heave = (float) jsonObject.getDouble("heave");

        JSONArray angVelArray = jsonObject.getJSONArray("angVel");
        for (int i = 0; i < 3; i++) {
            angVel[i] = (float) angVelArray.getDouble(i);
        }

        JSONArray eulerArray = jsonObject.getJSONArray("euler");
        for (int i = 0; i < 3; i++) {
            euler[i] = (float) eulerArray.getDouble(i);
        }
    }

    @Override
    public JSONObject toJsonObject() throws JSONException {

        JSONObject result = new JSONObject();
        result.put("timestamp", timestamp);
        result.put("localtime", localtime);

        JSONArray gyrArray = new JSONArray();
        for (int i = 0; i < 3; i++) {
            gyrArray.put(Float.isNaN(gyr[i]) ? -1f : gyr[i]);
        }
        result.put("gyr", gyrArray);

        JSONArray accArray = new JSONArray();
        for (int i = 0; i < 3; i++) {
            accArray.put(Float.isNaN(acc[i]) ? -1f : acc[i]);
        }
        result.put("acc", accArray);

        JSONArray magArray = new JSONArray();
        for (int i = 0; i < 3; i++) {
            magArray.put(Float.isNaN(mag[i]) ? -1f : mag[i]);
        }
        result.put("mag", magArray);

        JSONArray quatArray = new JSONArray();
        for (int i = 0; i < 4; i++) {
            quatArray.put(Float.isNaN(quat[i]) ? -1f : quat[i]);
        }
        result.put("quat", quatArray);

        JSONArray linAccArray = new JSONArray();
        for (int i = 0; i < 3; i++) {
            linAccArray.put(Float.isNaN(linAcc[i]) ? -1f : linAcc[i]);
        }
        result.put("linAcc", linAccArray);

        result.put("imuId", imuId);
        result.put("frameNumber", frameNumber);
        result.put("pressure", pressure);
        result.put("altitude", altitude);
        result.put("batteryLevel", batteryLevel);
        result.put("batteryVoltage", batteryVoltage);
        result.put("chargingStatus", chargingStatus);
        result.put("temperature", temperature);
        result.put("heave", heave);

        JSONArray angVelArray = new JSONArray();
        for (int i = 0; i < 3; i++) {
            angVelArray.put(Float.isNaN(angVel[i]) ? -1f : angVel[i]);
        }
        result.put("angVel", angVelArray);

        JSONArray eulerArray = new JSONArray();
        for (int i = 0; i < 3; i++) {
            eulerArray.put(Float.isNaN(euler[i]) ? -1f : euler[i]);
        }
        result.put("euler", eulerArray);

        return result;
    }

    @NonNull
    @Override
    public String toString() {
        return super.toString();
    }
}
